﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using System.ComponentModel;
using CCH.Entity;

namespace CCH.DAO
{
    [DataObject(true)]
    public class youtubeDAO
    {
        private Entities model = new Entities();

        #region 取資料

        public IQueryable<youtube> get_youtube_Data()
        {
            return from d in model.youtube
                   where d.state == "1"
                   orderby d.createtime descending
                   select d;
        }
        public IQueryable<youtube> get_youtube_Data(int startRowIndex, int maximumRows)
        {
            return get_youtube_Data().Skip(startRowIndex).Take(maximumRows);
        }
        public int get_youtube_DataCount()
        {
            return get_youtube_Data().Count();
        }

        public IQueryable<youtube> Get_youtube_AllData(int you_clano)
        {
            if (you_clano == 0)
            {
                return from d in model.youtube
                       where d.state == "1"
                       orderby d.createtime descending
                       select d;
            }
            else
            {
                return from d in model.youtube
                       where d.state == "1" &&d.you_clano == you_clano
                       orderby d.createtime descending
                       select d;
            }
        }

        public IQueryable<youtube> Get_youtube_AllData(int you_clano,int startRowIndex, int maximumRows)
        {
            return Get_youtube_AllData(you_clano).Skip(startRowIndex).Take(maximumRows);
        }

        #endregion

        public int Get_youtube_AllData_Count(int you_clano)
        {
            return Get_youtube_AllData(you_clano).Count();
        }

        public youtube Get_youtube(int you_no)
        {
            return (from d in model.youtube where d.you_no == you_no select d).FirstOrDefault();
        }

        public void Add_youtube(youtube d)
        {
            model.youtube.AddObject(d);
            Update();
        }

        public void Delete_youtube(int you_no, int peo_uid)
        {
            youtube d = Get_youtube(you_no);
            d.state = "2";
            d.createuid = peo_uid;
            d.createtime = DateTime.Now;
            Update();
        }

        public void Update()
        {
            model.SaveChanges();
        }
    }
}
